package com.ffapp.demo.greendao;


import com.alibaba.fastjson.annotation.JSONField;
import com.ffapp.demo.bean.WarnMapData;
import com.ffapp.demo.bean.WarnMapDataDao;
import com.ffapp.demo.bean.WarnMapListBean;

import java.util.List;

import my.LogUtil;

public class GreenDaoUtils {

    private static WarnMapDataDao warnMapDataDao = GreenDaoManager.getInstance().getmDaoSession().getWarnMapDataDao();

    public static final String TYPE_WATER = "water" //水源管理，包含消火栓、水源
            , TYPE_STATION = "station", TYPE_UNIT = "unit", TYPE_BUILDING = "build", TYPE_TRAIN = "train", TYPE_RISK = "risk"//单位隐患
            , TYPE_USER = "user"//未使用
            , TYPE_PERSON = "person"//仅供警情地图使用  ——》人员
            , TYPE_CAR = "car"//仅供警情地图使用   ——》车辆
            , TYPE_RECAR = "recar"//仅供警情地图使用   ——》警情相关车辆
            , TYPE_SQUADRON = "squadron"//仅供警情地图使用   ——》获取中队列表（实时获取，不加入基础数据）
            ;

    /**
     * 全量更新
     **/
    public static void WarnMapDataInsertData(WarnMapListBean infoBean) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        WarnMapData warnMapData = infoBean.getList().get(0);
        String map_type = warnMapData.getMap_type();
        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Map_type.eq(map_type)).buildDelete().executeDeleteWithoutDetachingEntities();
        for (WarnMapData data : infoBean.getList()) {
            warnMapDataDao.insertOrReplace(data);
        }
    }

    /**
     * 增量更新
     **/
    public static void WarnMapDataInsertData(WarnMapListBean infoBean, String map_type, GreenDaoInserCallBack callBack) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
//        warnMapDataDao.insertOrReplaceInTx(infoBean.getList());
//        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Deleted.eq("1"),WarnMapDataDao.Properties.Map_type.eq(map_type)).buildDelete().executeDeleteWithoutDetachingEntities();
        for (WarnMapData data : infoBean.getList()) {
            data.setMap_type(map_type);
//            if (!TextUtils.isEmpty(data.getDeleted()) && data.getDeleted().equals("1")) {
//                warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Id.eq(data.getId()), WarnMapDataDao.Properties.Map_type.eq(data.getMap_type())).buildDelete().executeDeleteWithoutDetachingEntities();
//            } else {
//                warnMapDataDao.insertOrReplace(data);
//            }
        }
        warnMapDataDao.insertOrReplaceInTx(infoBean.getList());
        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Deleted.eq("1"), WarnMapDataDao.Properties.Map_type.eq(map_type)).buildDelete().executeDeleteWithoutDetachingEntities();
        callBack.onDataInsert(infoBean.getList().size());
    }

    /**
     * 增量更新
     **/
    public static void WarnMapDataInsertData(WarnMapListBean infoBean, String map_type) {
        if (infoBean == null || infoBean.getList().size() == 0) return;
        WarnMapData warnMapData = infoBean.getList().get(0);
//        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Type.eq(type)) .buildDelete().executeDeleteWithoutDetachingEntities();
        for (WarnMapData data : infoBean.getList()) {
            data.setMap_type(map_type);
        }
        warnMapDataDao.insertOrReplaceInTx(infoBean.getList());
        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Deleted.eq("1"), WarnMapDataDao.Properties.Map_type.eq(map_type)).buildDelete().executeDeleteWithoutDetachingEntities();

    }



    public static WarnMapListBean getListBeanHydrant() {
        List<WarnMapData> list = warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Map_type.eq(TYPE_WATER), WarnMapDataDao.Properties.AuditStatus.eq("1"), WarnMapDataDao.Properties.Status.eq("1"), WarnMapDataDao.Properties.Type.eq(WarnMapData.WATER_TYPE_HYDRANT)).build().list();
        WarnMapListBean warnMapListBean = new WarnMapListBean();
        for (WarnMapData data : list) {
                warnMapListBean.getList().add(data);
        }
        LogUtil.i("getListBeanHydrant-->" + warnMapListBean.getList().size());
        return warnMapListBean;
    }

    public static long getListBeanCountByType(String type) {
        return warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Map_type.eq(type)).buildCount().count();
    }

    public static WarnMapListBean getListBean() {
        List<WarnMapData> list = warnMapDataDao.queryBuilder().build().list();
        WarnMapListBean warnMapListBean = new WarnMapListBean();
        for (WarnMapData data : list) {
            warnMapListBean.getList().add(data);
        }
        return warnMapListBean;
    }

    public static long getListBeanCount() {
        LogUtil.i("getListBeanCount--->" + warnMapDataDao.count());
        return warnMapDataDao.count();
    }



    public static WarnMapData getWarnMapDataById(String id) {
//        WarnMapData warnMapData =  warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Id.eq(id)).build().unique();
//        LogUtil.i("WarnMapData---->"+(warnMapData == null));
        List<WarnMapData> list = warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Id.eq(id)).build().list();
        LogUtil.i("WarnMapData---list->" + list.size());
        return list.size() > 0 ? list.get(0) : null;
    }

    public static void deleteByMapType(String map_type) {
        warnMapDataDao.queryBuilder().where(WarnMapDataDao.Properties.Map_type.eq(map_type)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public static void deleteAll() {
        warnMapDataDao.deleteAll();
    }

//
//    public void clearAll(){
//        alarmInfoBeanDao.deleteAll();
//        deviceCallBeanDao.deleteAll();
//        EventBus.getDefault().post(new GreenDaoUtils.EventDateChange(EventDateChange.TYPE_ALL));
//    }

    public static class EventDateChange {
        public static final String TYPE_ALL = "99";
        public String type;

        public EventDateChange(String type) {
            this.type = type;
        }
    }

    public interface GreenDaoInserCallBack {
        void onDataInsert(int count);
    }

    @JSONField(serialize = false)
    public static String getTypeName(String map_type) {
        switch (map_type) {
            case GreenDaoUtils.TYPE_STATION:
                return "微型消防站";
            case GreenDaoUtils.TYPE_WATER:
                return "水源管理";
            case GreenDaoUtils.TYPE_BUILDING:
                return "建筑信息";
            case GreenDaoUtils.TYPE_UNIT:
                return "单位信息";
            case GreenDaoUtils.TYPE_TRAIN:
                return "单位演练";
            case GreenDaoUtils.TYPE_RISK:
                return "单位隐患";
            default:
                return "未知";
        }
    }
}
